# 购物系统业务需求V1
# .0：（需要使用到列表技术）
# 购物：
# 1.
# 用户的账户里有钱
# 2.
# 用户可推一个空的购物车进商场
# 3.
# 开始购物，将自己想要的商品添加购物车
# 4.
# 打印购物小条
#
# 程序和数据流程梳理分析：
# 购前：
# 钱要有：money
# 有足够商品：列表来存储list
# 有购物车：列表list
#
# 购中：
# 是否有这个商品：
# 有
# 钱够不够：
# 够：
# 添加到购物车！
# 钱减去商品价格！
# 恭喜，购买成功！您的卡余额还剩xxx
# 不够：
# 穷鬼，钱不够！请到其他超市购买！
# 没有：
# 没有这个商品，别瞎弄！
# 购后：
# 输入Q，q: 退出：
# 打印购物小条！

def shop(money, commodity):
    cart = []
    balance = money
    while True:
        print("目前购物车内有:")
        for ca in cart:
            print(ca["name"], ca["numb"])
        print(f"资金余额{balance}是否继续购买商品？：")
        text1 = input()
        if text1 == "是":
            if balance == 0:
                print("资金不足，购物结束")
                print("目前购物车内有:")
                for ca in cart:
                    print(ca["name"], ca["numb"])
                break
            while True:
                name = input("请输入商品名称,退出购买请输入0:")
                if name == "0":
                    break
                a = False
                for com in commodity:
                    if com["name"] == name:
                        a = True
                        if com["quantity"] == 0:
                            print("商品库存为0,请选择其他商品")
                            break
                        if com["price"] > balance:
                            print("余额不足购买此商品，请选择其他商品")
                            break
                        try:
                            num = int(input("请输入购买数量"))
                        except ValueError:
                            print("请输入正确的整数数量！")
                            break
                        else:
                            if num <= 0:
                                print("数量必须大于等于1")
                                break
                            if num >= com["quantity"]:
                                print("库存不足！")
                                break
                            money_num = num * com["price"]
                            if money_num > balance:
                                print("余额不足！")
                                break

                            cart.append({"name": com["name"], "numb": num})
                            com["quantity"] -= num
                            balance -= money_num
                            print(f"成功购买 {com['name']} : {num} 件，花费 {money_num} 元")
                            break
                if a == False:
                    print("请输入存在的商品名称")
                    continue

        elif text1 == "否":
            print(f"购物结束！目前资金余额{balance}")
            print("目前购物车内有:")
            for ca in cart:
                print(ca["name"], ca["numb"])
            break

        else:
            print("请输入是或者否！")
            continue
    return money


commodity = [{"name": "苹果", "price": 1, "quantity": 50},
             {"name": "香蕉", "price": 10, "quantity": 20},
             {"name": "菠萝", "price": 14, "quantity": 30},
             {"name": "橙子", "price": 20, "quantity": 0},
             {"name": "手机", "price": 1000, "quantity": 2}]
for com in commodity:
    print(com["name"], com["price"], com["quantity"])
while True:
    text = input("是否购物？请回答是或否：")
    if text == "是":
        try:
            money = float(input("请输入所带金额："))
        except ValueError:
            print("请输入正确的数字金额！")
            continue
        else:
            shop(money, commodity)
            break
    elif text == "否":
        print("购物结束，退出")
        break
    else:
        print("请输入是或否！")
        continue
#
# 购物系统业务需求V2
# .0：
# 1.
# 现在用户在进商场之前需要从10张优惠券中，随机抽取一张优惠券。
# 比如抽到一个空凋的8折优惠券，后面在购物的过程中，空凋就可以按照8折后的价格来购买。
# 2.
# 优化购物小条的人性化打印。
# 购物时间，卡余额，购物数量，购买总商品的价值。
# 3.
# 添加退货功能。在购买途中，某个商品不要了，退回到购物架中！

def shop(money, commodity, Zname, yh):
    cart = []
    balance = money
    while True:
        print("目前购物车内有:")
        for ca in cart:
            print(ca["name"], ca["numb"])
        print(f"共花费金额{money - balance}")
        print(f"资金余额{balance}是否继续购买商品？：")
        text1 = input()
        if text1 == "是":
            if balance == 0:
                print("资金不足，购物结束")
                print("目前购物车内有:")
                for ca in cart:
                    print(ca["name"], ca["numb"])
                break
            while True:
                name = input("请输入商品名称,退货请输入1，退出购买请输入0:")
                if name == "1":
                    tname = input("输入要退的货名：")
                    ss = False
                    for car in cart:
                        if tname == car["name"]:
                            ss = True
                            for com in commodity:
                                if com["name"] == tname:
                                    com["quantity"] += car["numb"]  # 恢复库存
                                    if tname == Zname:
                                        s = yh / 10
                                        price = com["price"] * s
                                    else:
                                        price = com["price"]
                                    money_numm = car["numb"] * price
                                    balance += money_numm
                                    cart.remove(car)
                                    print(f"成功退货 {tname} × {car['numb']}，退还 {money_numm:.2f} 元")
                                    break
                    break
                    if ss == False:
                        print("您还未买过此商品！")
                        continue
                if name == "0":
                    break
                a = False
                for com in commodity:
                    if com["name"] == name:
                        a = True
                        if com["quantity"] == 0:
                            print("商品库存为0,请选择其他商品")
                            break
                        if com["price"] > balance:
                            print("余额不足购买此商品，请选择其他商品")
                            break
                        try:
                            num = int(input("请输入购买数量"))
                        except ValueError:
                            print("请输入正确的整数数量！")
                            break
                        else:
                            if num <= 0:
                                print("数量必须大于等于1")
                                break
                            if num >= com["quantity"]:
                                print("库存不足！")
                                break
                            if name == Zname:
                                a = yh / 10
                                co = com["price"] * a
                            else:
                                co = com["price"]
                            money_num = num * co
                            if money_num > balance:
                                print("余额不足！")
                                break

                            cart.append({"name": com["name"], "numb": num})
                            com["quantity"] -= num
                            balance -= money_num
                            print(f"成功购买 {com['name']} : {num} 件，花费 {money_num} 元")
                            break
                if a == False:
                    print("请输入存在的商品名称")
                    continue

        elif text1 == "否":
            print(f"购物结束！目前资金余额{balance}")
            print("目前购物车内有:")
            for ca in cart:
                print(ca["name"], ca["numb"])
            break

        else:
            print("请输入是或者否！")
            continue
    return money


coupon = [{"id": 1, "name": "苹果", "yh": 5},
          {"id": 2, "name": "香蕉", "yh": 5},
          {"id": 3, "name": "菠萝", "yh": 5},
          {"id": 4, "name": "牙刷", "yh": 5},
          {"id": 5, "name": "牙膏", "yh": 5},
          {"id": 6, "name": "糖", "yh": 5},
          {"id": 7, "name": "纸巾", "yh": 8},
          {"id": 8, "name": "眼镜", "yh": 5},
          {"id": 9, "name": "饮料", "yh": 5},
          {"id": 10, "name": "水", "yh": 5}]
commodity = [{"name": "苹果", "price": 1, "quantity": 50},
             {"name": "香蕉", "price": 10, "quantity": 20},
             {"name": "菠萝", "price": 14, "quantity": 30},
             {"name": "橙子", "price": 20, "quantity": 0},
             {"name": "手机", "price": 1000, "quantity": 2},
             {"name": "牙刷", "price": 5, "quantity": 30},
             {"name": "牙膏", "price": 2, "quantity": 30},
             {"name": "糖", "price": 2, "quantity": 100},
             {"name": "纸巾", "price": 20, "quantity": 50},
             {"name": "眼镜", "price": 150, "quantity": 5},
             {"name": "饮料", "price": 5, "quantity": 50},
             {"name": "水", "price": 1, "quantity": 100}]
for com in commodity:
    print(com["name"], com["price"], com["quantity"])
while True:
    text = input("是否购物？请回答是或否：")
    if text == "是":
        import random

        nummm = random.randint(1, 10)
        for cou in coupon:
            if nummm == cou["id"]:
                print(f"恭喜抽中{cou["name"]}的折扣卷，折扣额度{cou["yh"]}折")
                Zname = cou["name"]
                yh = cou["yh"]
        try:
            money = float(input("请输入所带金额："))
        except ValueError:
            print("请输入正确的数字金额！")
            continue
        else:
            shop(money, commodity, Zname, yh)
            break
    elif text == "否":
        print("购物结束，退出")
        break
    else:
        print("请输入是或否！")
        continue







